<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2007 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version
**********************************************/
if (!defined('CPG_NUKE')) { exit; }
if (php_sapi_name() == 'cli' || empty($_SERVER['PHP_SELF'])) { die('This script cannot be accessed through the command line'); }

require('includes/cmsinit.inc');
if (DF_HTTP_SSL_REQUIRED && 'https' !== DOMAIN_PROTOCOL) {
	URL::redirect('https://'. DOMAIN_NAME. URL::uri());
}
header('Last-Modified: '.date('D, d M Y H:i:s', time()).' GMT');
header('X-Content-Type-Options: nosniff');
// Only Internet Explorer needs the useless P3P header to accept cookies
header('P3P: CP="CAO DSP COR CURa ADMa DEVa OUR IND PHY ONL UNI COM NAV INT DEM PRE"');

$Module = new Module('op', 'file');
$module_name = $Module->name;

/******************************************************************************
  Load the theme template system
*******************************************************************************/
$CPG_SESS['theme'] = $ThemeSel = $cpgtpl->theme;
# Load template handler
$template =& $cpgtpl;

# backward compatibility
global $pagetitle, $modheader;
$pagetitle = $modheader = '';

Dragonfly_Output_Js::add('includes/javascript/poodle.js');
if ($MAIN_CFG['global']['admin_help']) {
	Dragonfly_Output_Js::add('includes/javascript/infobox.js');
}

// if (empty($_SERVER['HTTPS']) && $op != 'logout') { URL::redirect('https://'.$MAIN_CFG['server']['domain'].URL::uri()); }
if (isset($_GET['hideallblocks'])) { $Module->sides = Blocks::NONE; }

Dragonfly_Page::setTitle(_ADMINISTRATION);

if (!is_admin()) {
	if (!$db->count('admins')) {
		if (!isset($_POST['name'])) {
			$K = Dragonfly::getKernel();
			$TPL = $K->OUT;
			$TPL->login_action = DOMAIN_ADMIN;
			if ($K->CFG->global->sec_code & 1) {
				$TPL->login_image = generate_secimg(7);
			}
			$TPL->display('admin/account/create-first');
			require('footer.php');
		} else if (isset($_POST['fop']) && $_POST['fop'] == 'create_first') {
			if (preg_match('#^[0-9]#', $_POST['pwd']) && preg_match('#[a-z]#', $_POST['pwd']) && preg_match('#[A-Z]#', $_POST['pwd'])) {
				$db->TBL->admins->insert(array(
					'aid'   => $_POST['name'],
					'email' => $_POST['email'],
					'pwd'   => \Poodle\Auth::hashPassword($_POST['pwd']),
					'radminsuper' => 1
				));
				if (!empty($_POST['user_new'])) {
					$user_id = $db->TBL->users->insert(array(
						'username'         => $_POST['name'],
						'user_nickname_lc' => mb_strtolower($_POST['name']),
						'user_email'       => $_POST['email'],
						'user_avatar'      => $MAIN_CFG['avatar']['default'],
						'user_regdate'     => time(),
						'theme'            => $MAIN_CFG['global']['Default_Theme'],
						'user_level'       => 2,
						'user_timezone'    => date_default_timezone_get(),
					), 'user_id');
					\Poodle\Identity\Search::byID($user_id)->updateAuth(1, $_POST['name'], $_POST['pwd']);
				}
				login();
			} else {
				cpg_error(_PASSWORD_MALFORMED);
			}
		}
		exit;
	}
}

function login()
{
	$GLOBALS['pagetitle'] .= ' '._BC_DELIM.' '._ADMINLOGIN;
	$K = Dragonfly::getKernel();
	$TPL = $K->OUT;
	$TPL->login_action = DOMAIN_ADMIN;
	if ($K->CFG->global->sec_code & 1) {
		$TPL->login_image = generate_secimg(7);
	}
	$TPL->display('admin/account/login');
	require('footer.php');
}

/***********************************************************************************
 Echo the big graphical menu, function called by the admin modules
	$cat: Which menucategory to show, default = all
************************************************************************************/
function GraphicAdmin($cat='all')
{
	trigger_deprecated();
}

$op = (!empty($_GET['op']) ? $_GET['op'] : (isset($_POST['op']) ? $_POST['op'] : 'index'));
if ($MAIN_CFG['global']['admingraphic'] & 8 || strtolower($op) == 'forums') {
	Dragonfly_Output_Js::add('includes/javascript/JSCookMenu.js');
	Dragonfly_Output_Js::add('themes/admin/javascript/cookmenu.js');
	Dragonfly_Output_Css::add('cookmenu');
}
global $CPG_SESS;
if ($op == 'logout') {
	unset($CPG_SESS['admin']);
	$redir = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $mainindex;
	cpg_error(_YOUARELOGGEDOUT, _ADMINMENU_LOGOUT, $redir);
}
else if (is_admin()) {
	include($Module->chroot.$Module->file);
	if (defined('HEADER_OPEN')) { require_once('footer.php'); }
	else if (!XMLHTTPRequest) { cpg_error("The requested file, {$Module->file}, didn't output data correctly"); }
}
else {
	if (SEARCHBOT) {
		Dragonfly_Net_Http::headersFlush(403);
	}
	login();
}
